<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
    <script src="/js/jquery-3.3.1.min.js"></script>

    <script src="/ueditor/third-party/jquery-1.10.2.js"></script>
    <!-- 配置文件 -->
    <script src="/ueditor/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script src="/ueditor/ueditor.all.js"></script>
    <script src="/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script src="/ueditor/ueditor.parse.js"></script>

</head>
<body>
<ul class="layui-nav layui-nav-tree layui-nav-side" lay-filter="demo">
    <li class="layui-nav-item">
        <a href="/index">发帖</a>
    </li>
    <li class="layui-nav-item">
        <a href="/detail">查看帖子</a>
    </li>
    <li class="layui-nav-item"><a href="/ueditor">UEditor</a></li>
</ul>
<form>
    <div class="layui-row layui-form" style="margin-left: 250px;">
        <div class="layui-form-item">
            <div style="margin-top: 40px; width: 500px;">
                <label for="L_title" class="layui-form-label" style="background-color: #00F7DE">标题</label>
                <div class="layui-input-block">
                    <input type="text" id="L_title" name="title" required lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div style="margin-top: 20px;">
                <textarea id="faq_edit" type="text" style="height: 400px; margin-right: 100px;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" lay-filter="add" lay-submit>立即发布</button>
        </div>
    </div>
</form>
<script type="application/javascript">

    layui.define(function () {
        let ue = UE.getEditor('faq_edit');

        //自定义请求地址
        UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
        UE.Editor.prototype.getActionUrl = function(action) {
            if (action === 'uploadimage' || action === 'uploadvideo' || action === 'uploadfile') {
                return this._bkGetActionUrl.call(this, action);
            } else if (action === 'uploadscrawl') {
                return '/ueditor/uploadscrawl?action=uploadscrawl';
            } else if (action === 'listimage') {
                return '/ueditor/listimage?action=listimage';
            }else if (action === 'listfile') {
                return '/ueditor/listfile?action=listfile';
            }else  {
                return this._bkGetActionUrl.call(this, action);
            }
        }

        layui.use('form', function () {
            var form = layui.form, layer = layui.layer, $ = layui.jquery;

            form.on('submit(add)', function (data) {
                layer.load({
                    shade: 0.5,
                    time: 0,
                });

                console.log("L_title: " + document.getElementById("L_title").value);
                console.log("content: " + ue.getContent());

                setTimeout(function () {
                    //ajax提交信息
                    $.ajax({
                        type: "get",
                        async: false,
                        url: "/forum/add_forum",
                        data: {
                            "title": document.getElementById("L_title").value,
                            "content": ue.getContent()
                        },
                        dataType: 'json',
                        success: function (msg) {
                            var index = layer.load();
                            if ("Success" === msg.message) {
                                layer.close(index);
                                layer.alert('发布帖子成功！！！', function (index) {
                                    //do something
                                    window.location.reload();
                                    layer.close(index);
                                });

                            }
                        },
                        error: function (msg) {
                            // 请求失败时执行该函数
                            var index = layer.load();
                            layer.close(index);
                            layer.alert('请求数据失败！！！', function (index) {
                                //do something
                                layer.close(index);
                            });
                        }
                    });
                }, 500);

                return false;
            });
        });
    });
</script>

</body>
</html>